/*
 * $Id: SendToProcessor.java,v 1.1 2010/03/03 20:34:34 navdeep_mahajan Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

/**
 * 
 */
package com.ge.healthcare.autosc.common.util;

import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.XAConnection;
import javax.jms.XAConnectionFactory;
import javax.jms.XASession;
import javax.rmi.PortableRemoteObject;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.ActTask;
import com.ge.healthcare.autosc.common.database.pojo.CfgTaskCategoryInfo;

/**
 * @author 212042764
 * 
 */
public class SendToProcessor {
	// Defines the JMS connection factory for the topic.
	private String jmsFactory = null;
	private ServiceLocator serviceLocator = null;

	/**
	 * Just because spring likes it
	 */
	public SendToProcessor() {
		super();
	}

	/**
	 * Sends the task info object to category queue
	 * 
	 * @param taskInfo
	 *            - Task info object
	 * @param rtvdPrcToTask
	 *            - Process info for the given task
	 * @throws JMSException
	 *             - Failed to send the message
	 */
	public void sendToCategoryQueue(ActTask taskInfo,
			CfgTaskCategoryInfo taskCategoryInfo) throws JMSException {
		this.sendToCategoryQueue(taskInfo, taskCategoryInfo, null);
	}

	/**
	 * Sends the task info object to category queue
	 * 
	 * @param taskInfo
	 *            - Task info object
	 * @param rtvdPrcToTask
	 *            - Process info for the given task
	 * @throws JMSException
	 *             - Failed to send the message
	 */
	public void sendToCategoryQueue(ActTask taskInfo,
			CfgTaskCategoryInfo taskCategoryInfo, String providerURL)
			throws JMSException {
		String methodName = "sendObjectToProcessor";
		XAConnectionFactory queueConnFactory = null;
		Queue queue = null;

		if (ASCUtil.isNullOrEmpty(providerURL)) {
			queueConnFactory = (XAConnectionFactory) PortableRemoteObject
					.narrow(serviceLocator.getService(jmsFactory),
							XAConnectionFactory.class);
			queue = (Queue) PortableRemoteObject.narrow(serviceLocator
					.getService(taskCategoryInfo.getQueueName()), Queue.class);
		} else {
			queueConnFactory = (XAConnectionFactory) PortableRemoteObject
					.narrow(serviceLocator.getService(providerURL, jmsFactory),
							XAConnectionFactory.class);
			queue = (Queue) PortableRemoteObject.narrow(serviceLocator
					.getService(providerURL, taskCategoryInfo.getQueueName()),
					Queue.class);
		}

		XAConnection queueConnection = null;
		XASession queueSession = null;
		MessageProducer producer = null;
		try {
			queueConnection = queueConnFactory.createXAConnection();
			queueSession = queueConnection.createXASession();
			producer = queueSession.createProducer(queue);
			ObjectMessage objMessage = queueSession
					.createObjectMessage(taskInfo);
			producer.send(objMessage);
			ASCLogger.debug(this.getClass(), methodName,
					"Message sent successfully");
		} finally {
			this.cleanUp(producer, queueSession, queueConnection);
		}
	}

	/**
	 * Release the queue connections
	 */
	public void cleanUp(MessageProducer queueSender, XASession queueSession,
			XAConnection queueConnection) {
		try {
			if (!ASCUtil.isNullOrEmptyObject(queueSender)) {
				queueSender.close();
				queueSender = null;
			}
		} catch (Throwable e) {
			queueSender = null;
		}
		try {
			if (!ASCUtil.isNullOrEmptyObject(queueSession)) {
				queueSession.close();
				queueSession = null;
			}
		} catch (Throwable e) {
			queueSession = null;
		}
		try {
			if (!ASCUtil.isNullOrEmptyObject(queueConnection)) {
				queueConnection.close();
				queueConnection = null;
			}
		} catch (Throwable e) {
			queueConnection = null;
		}
	}

	/**
	 * @param serviceLocator
	 *            the serviceLocator to set
	 */
	public void setServiceLocator(ServiceLocator serviceLocator) {
		this.serviceLocator = serviceLocator;
	}

	/**
	 * @param jmsFactory
	 *            the jmsFactory to set
	 */
	public void setJmsFactory(String jmsFactory) {
		this.jmsFactory = jmsFactory;
	}
}
